package org.lemon.db.store;

import org.lemon.common.security.SHA256;
import org.lemon.common.util.StringUtils;

/**
 * @since 2024-12-11 11:32
 */
public class BlockId {
    private String fileName;
    private long blkNum;

    public BlockId(String fileName, long blkNum) {
        this.fileName = fileName;
        this.blkNum = blkNum;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public long getBlkNum() {
        return blkNum;
    }

    public void setBlkNum(long blkNum) {
        this.blkNum = blkNum;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        BlockId blockId = (BlockId) o;
        return blkNum == blockId.blkNum && fileName.equals(blockId.fileName);
    }

    public String hash(){
        byte[] b = SHA256.hash((fileName + blkNum).getBytes(),false);
        return StringUtils.convertBytesToHex(b);
    }

    @Override
    public int hashCode() {
        return fileName.hashCode() + (int)blkNum;
    }
}
